package com.yunxitest.yunxitest.utils;

import android.text.TextUtils;
import android.util.Log;

public class L {
    private static boolean showAll = true;

    private static boolean showV = true;
    private static boolean showD = true;
    private static boolean showI = true;
    private static boolean showW = true;
    private static boolean showE = true;


    private static int count = 0;
    /**
     * 得到tag（所在类.方法（L:行））
     */
    private static String generateTag(String t) {
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[4];
        String callerClazzName = stackTraceElement.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        String tag = "%s.%s(:%d)";
        tag = String.format(tag, new Object[]{callerClazzName, stackTraceElement.getMethodName(), Integer.valueOf(stackTraceElement.getLineNumber())});
        //给tag设置前缀
        tag = TextUtils.isEmpty(t) ? tag : t + ":" + tag;
        return tag;
    }

    public static void v(String t, String msg) {
        if (showV && showAll) {
            String tag = generateTag(t);
            Log.v(tag, msg);
        }
    }


    public static void d(String t, String msg) {
        if (showD && showAll) {
            String tag = generateTag(t);
            Log.d(tag, msg);
        }
    }

    public static void dd(String t, String msg,int countNum) {
        if (showD && showAll) {
            count ++;
            if(count >= countNum){
                count = 0;
                String tag = generateTag(t);
                Log.d(tag, msg);
            }
        }
    }

    public static void d(String t, String msg, Throwable tr) {
        if (showD && showAll) {
            String tag = generateTag(t);
            Log.d(tag, msg, tr);
        }
    }

    public static void i(String t, String msg) {
        if (showI && showAll) {
            String tag = generateTag(t);
            Log.i(tag, msg);
        }
    }

    public static void i(String t, String msg, Throwable tr) {
        if (showI && showAll) {
            String tag = generateTag(t);
            Log.i(tag, msg, tr);
        }
    }

    public static void w(String t, String msg) {
        if (showW && showAll) {
            String tag = generateTag(t);
            Log.w(tag, msg);
        }
    }

    public static void w(String t, String msg, Throwable tr) {
        if (showW && showAll) {
            String tag = generateTag(t);
            Log.w(tag, msg, tr);
        }
    }

    public static void e(String t, String msg) {
        if (showE && showAll) {
            String tag = generateTag(t);
            Log.e(tag, msg);
        }
    }

    public static void e(String t, String msg, Throwable tr) {
        if (showE && showAll) {
            String tag = generateTag(t);
            Log.e(tag, msg, tr);
        }
    }


}
